package com.kfd.controller;

import com.kfd.bean.*;
import com.kfd.exception.DuplicateCodeException;
import com.kfd.exception.DuplicateNumberException;
import com.kfd.mvc.ResponseBody;
import com.kfd.service.EadminService;
import com.kfd.service.ExpressService;
import com.kfd.util.DateFormatUtil;
import com.kfd.util.JSONUtil;
import com.kfd.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CourierController {

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = EadminService.console();
        Message message = new Message();
        if(data.size() == 0){
            message.setStatus(-1);
        }else{
            message.setStatus(0);
        }
        message.setData(data);
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/courier/findAll.do")
    public String findAll(HttpServletRequest request, HttpServletResponse response){
        //获取查询数据的起始索引
        String offset = request.getParameter("offset");
        //获取要查询的数据量
        String pageNumber = request.getParameter("pageNumber");

        List<Eadmin> all = EadminService.findAll(true, Integer.valueOf(offset), Integer.valueOf(pageNumber),1);
        List<BootstrapTableEadmin> list = new ArrayList<>();

        for (Eadmin eadmin : all) {
            List<Express> bySysPhone = ExpressService.findBySysPhone(eadmin.getUserPhone());

            BootstrapTableEadmin bootstrapTableEadmin = new BootstrapTableEadmin(eadmin.getId(),eadmin.getName(),eadmin.getUsername(),eadmin.getPassword(),eadmin.getLoginip(),eadmin.getLogintime(),String.valueOf(eadmin.getCreatetime()),eadmin.getUserPhone(),eadmin.getStatus(),bySysPhone==null?0:bySysPhone.size());
            list.add(bootstrapTableEadmin);
        }
        //bootstrap分页对象
        ResultData resultData = new ResultData();
        resultData.setRows(list);
        resultData.setTotal(list.size());
        //返回对象
        return JSONUtil.toJSON(resultData);
    }

    @ResponseBody("/user/findAll.do")
    public String userFindAll(HttpServletRequest request, HttpServletResponse response){
        //获取查询数据的起始索引
        String offset = request.getParameter("offset");
        //获取要查询的数据量
        String pageNumber = request.getParameter("pageNumber");

        List<Eadmin> all = EadminService.findAll(true, Integer.valueOf(offset), Integer.valueOf(pageNumber),0);
        List<BootstrapTableEadmin> list = new ArrayList<>();

        for (Eadmin eadmin : all) {
            BootstrapTableEadmin bootstrapTableEadmin = new BootstrapTableEadmin(eadmin.getId(),eadmin.getName(),eadmin.getUsername(),eadmin.getPassword(),eadmin.getLoginip(),eadmin.getLogintime(),String.valueOf(eadmin.getCreatetime()),eadmin.getUserPhone(),eadmin.getStatus());
            list.add(bootstrapTableEadmin);
        }
        //bootstrap分页对象
        ResultData resultData = new ResultData();
        resultData.setRows(list);
        resultData.setTotal(list.size());
        //返回对象
        return JSONUtil.toJSON(resultData);
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response) throws DuplicateCodeException, DuplicateNumberException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String idno = request.getParameter("idno");
        String password = request.getParameter("password");
        String ip = request.getRemoteAddr();
        boolean insert = EadminService.insert(new Eadmin(name, phone, password, ip, null, Timestamp.valueOf(DateFormatUtil.format(new Date())), phone, idno, "1"), 1);

        Message message = new Message();

        if(insert){
            message.setStatus(0);
        }else{
            message.setStatus(-1);
        }
        return JSONUtil.toJSON(message);
    }
    @ResponseBody("/user/insert.do")
    public String insertuser(HttpServletRequest request, HttpServletResponse response) throws DuplicateCodeException, DuplicateNumberException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String idno = request.getParameter("idno");
        String password = request.getParameter("password");
        String ip = request.getRemoteAddr();
        boolean insert = EadminService.insert(new Eadmin(name, phone, password, ip, null, Timestamp.valueOf(DateFormatUtil.format(new Date())), phone, idno, "0"), 0);

        Message message = new Message();

        if(insert){
            message.setStatus(0);
        }else{
            message.setStatus(-1);
        }
        return JSONUtil.toJSON(message);
    }
    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String phone = request.getParameter("phone");
        Eadmin byPhone = EadminService.findByPhone(phone, 1);
        Message message = new Message();
        if(byPhone == null){
            message.setStatus(-1);
            message.setResult("手机号不存在");
        }else{
            message.setStatus(0);
            message.setData(byPhone);
            message.setResult("查询成功");
        }
        return JSONUtil.toJSON(message);
    }
    // TODO 应该通知用户手机号修改了
    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String userphone = request.getParameter("userphone");
        String idNO = request.getParameter("idNO");
        String password = request.getParameter("password");
        Eadmin eadmin = new Eadmin();
        eadmin.setId(Integer.parseInt(id));
        eadmin.setName(name);
        eadmin.setUserPhone(userphone);
        eadmin.setIdNO(idNO);
        eadmin.setPassword(password);
eadmin.setStatus("1");
        boolean update = EadminService.update(eadmin);

        Message message = new Message();
        if(update){
            message.setStatus(0);
            message.setResult("修改成功！");
        }else{
            message.setStatus(-1);
            message.setResult("修改失败！");
        }
        return JSONUtil.toJSON(message);
    }
    @ResponseBody("/user/find.do")
    public String finduser(HttpServletRequest request, HttpServletResponse response){
        String phone = request.getParameter("phone");
        Eadmin byPhone = EadminService.findByPhone(phone, 0);
        Message message = new Message();
        if(byPhone == null){
            message.setStatus(-1);
            message.setResult("手机号不存在");
        }else{
            message.setStatus(0);
            message.setData(byPhone);
            message.setResult("查询成功");
        }
        return JSONUtil.toJSON(message);
    }
    // TODO 应该通知用户手机号修改了
    @ResponseBody("/user/update.do")
    public String updateuser(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String userphone = request.getParameter("userphone");
        String idNO = request.getParameter("idNO");
        String password = request.getParameter("password");
        Eadmin eadmin = new Eadmin();
        eadmin.setId(Integer.parseInt(id));
        eadmin.setName(name);
        eadmin.setUserPhone(userphone);
        eadmin.setIdNO(idNO);
        eadmin.setPassword(password);
        eadmin.setStatus("0");
        boolean update = EadminService.update(eadmin);

        Message message = new Message();
        if(update){
            message.setStatus(0);
            message.setResult("修改成功！");
        }else{
            message.setStatus(-1);
            message.setResult("修改失败！");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/log/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        Eadmin byId = EadminService.findById(Integer.parseInt(id));
        int result = 0;
        List<Express> byUserPhone = ExpressService.findByUserPhone(byId.getUserPhone(), 0);
        if(byUserPhone != null){
            result=1;//用户还有未取件的快递，删除失败；
        }
        List<Express> bySysPhone = ExpressService.findBySysPhoneNo(byId.getUserPhone(),"0");
        if(bySysPhone != null){
            result=2;//该快递员派送后还有用户未取件的快递，完成该快递员的订单后可以删除，删除失败；
        }
        boolean delete = false;
        if(result == 0){
            delete = EadminService.delete(Integer.parseInt(id), Integer.parseInt(status));
        }

        Message message = new Message();
        if(delete){
            message.setStatus(0);
            message.setResult("删除成功！");
        }else{
            message.setStatus(-1);
            if(result == 0){
                message.setResult("删除失败！");
            }else if(result == 1){
                message.setResult("用户还有未取件的快递，删除失败");
            }else if(result == 2){
                message.setResult("该快递员派送后还有用户未取件的快递，完成该快递员的订单后可以删除，删除失败");
            }

        }
        return JSONUtil.toJSON(message);

    }
}
